import Vue from "vue";
import {request} from "../../../util/request.js";
import "../../../style/basic.less";
import "./base.less";
import addAdmin from "./images/add-admin.png";
import dialog from "../../../util/dialog.js";
import multiSync from "../../../components/multiSync/multi-sync.js";
new Vue({
    el:"#louie",
    data:{
        admin_src:addAdmin,
        bind_admin_qrcode:"",  //绑定管理员二维码
        open_phone:0,
        coerce_phone:0,
        web_name:"",
        publish_red:0,
        defend:0,
        admin_uid:0,
        notice:"",
        source_notice:""  //原公告内容
    },
    components:{
        "multi-sync":multiSync
    },
    computed:{
        data:function(){
            let data = {
                web_name:this.web_name,
                publish_red:this.publish_red,
                defend:this.defend,
                admin_uid:this.admin_uid,
                notice:this.notice,
                open_phone:this.open_phone,
                coerce_phone:this.coerce_phone
            }
            if(this.notice !== this.source_notice){
                data.notice_time = parseInt(new Date().getTime()/1000);
            }
            return data;
        }
    },
    created:function(){
        this.get();
    },
    methods:{
        /**
         * 获取参数
         */
        get(){
            request("setting/easyFind",{
                return:["web_name","open_phone","publish_red","defend","admin_uid","notice","coerce_phone"]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        let data = res.data.data;
                        this.web_name = data.web_name;
                        this.open_phone = parseInt(data.open_phone);
                        this.coerce_phone = data.coerce_phone;
                        this.publish_red = data.publish_red;
                        this.defend = data.defend;
                        this.admin_uid = data.admin_uid;
                        this.source_notice = this.notice = data.notice;
                        if(parseInt(data.admin_uid) !== 0){
                            request("memberA/find",{
                                uid:data.admin_uid
                            })
                                .then((result)=>{
                                    if(result.data.status === "success"){
                                        this.admin_src = result.data.data.header;
                                    }
                                });
                        }
                    }
                });
        },
        /**
         * 设置参数
         */
        submit(){
            if(this.notice.length > 255){
                dialog.alert(this,"公告内容长度不能大于255");
                return false;
            }
            request("setting/set",{
                data:this.data
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        dialog.alert(this,"设置成功");
                    }else{
                        dialog.alert(this,"设置失败");
                    }
                });
        },
        /**
         * 防封选项改变
         */
        defendChange(){
            if(this.defend === '1'){
                request("setting/testDdfend")
                    .then((res)=>{
                        if(res.data.status === "error"){
                            dialog.alert(this,"系统未安装酷源防封模块");
                            this.defend = '0';
                        }
                    });
            }
        },
        /**
         * 点击绑定管理员
         */
        clickBindAdmin(){
            this.$modal.show('bind-admin-qrcode');
        },
        /**
         * 关闭绑定管理员弹窗
         */
        clickCloseBindAdmin(){
            this.$modal.hide('bind-admin-qrcode');
        },
        /**
         * 绑定管理员弹窗显示之前执行
         */
        beforeOpenAdminQrcode(){
            request("setting/getBindAdminQrcode")
                .then((res)=>{
                    if(res.data.status === "success"){
                        console.log(res)
                        this.bind_admin_qrcode = res.data.data;
                    }
                });
        },
        /**
         * 绑定管理员弹窗关闭
         */
        closeAdminQrcode(){
            request("setting/getTempAdmin")
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.admin_uid = res.data.data.uid;
                        this.admin_src = res.data.data.header;
                    }
                });
        }
    }
});